package com.wontube.smr.web.controller.i18n;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.web.client.IMovieWebHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * 电影控制器
 * 
 * @author hewc@wondershare.cn
 * 
 */
@Controller
@RequestMapping("/world/movie")
public class I18nMovieController {
	private final static int PAGE_SIZE = 30;
	private final static int RELATE_PAGE_SIZE = 6;	
	private final static String SORT_TIME = "time";
	
	@RequestMapping("")
	public void f1(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/world/movie/");		
	}
	
	@RequestMapping("/")
	public ModelAndView movieHome() {
		return movieAll();
	}
	
	@RequestMapping("/full-length")
	public void f2(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/world/movie/full-length/");		
	}

	@RequestMapping("/full-length/")
	public ModelAndView movieAll() {
		ModelAndView mv = getListMovieView(new QueryItemParams(true), "/i18n/movie/movie_index");
		mv.addObject("category", "full-length");		
		return mv;
	}
	
	@RequestMapping("/full-lengths")
	public ModelAndView movieAlls() {
		ModelAndView mv = new ModelAndView("/i18n/movie/movie_index");
		mv.addObject("category", "full-length");		
		mv.addObject("movieGenres", getI18nMovieGenres());
		return mv;
	}

	@RequestMapping("/full-length/query")
	public ModelAndView queryMovies(QueryItemParams params) {
		params.setNeedGenres(false);
		return getListMovieView(params, "/i18n/movie/movie_list");
	}
	
	@RequestMapping("/full-length/genre/{genre}")
	public ModelAndView queryMoviesGenre(@PathVariable("genre") String genre) {
		QueryItemParams  params = new QueryItemParams(true);
		params.setGenre(genre);
		ModelAndView mv = getListMovieView(params, "/i18n/movie/movie_index");
		mv.addObject("category", "full-length");
		return mv;
	}
	
	@RequestMapping("/full-length/alpha/{alpha}")
	public ModelAndView alpha(@PathVariable("alpha") String initial) {
		QueryItemParams  params = new QueryItemParams(true);
		params.setInitial(initial);
		ModelAndView mv = getListMovieView(params, "/i18n/movie/movie_index");
		mv.addObject("category", "full-length");
		return mv;
	}
	
	@RequestMapping("/full-length/{id}")
	public ModelAndView moviesDetail(@PathVariable("id") String movieId,
			HttpServletResponse response) throws IOException {
		return getFullMovieView("i18n/movie/detail/detail_index", movieId, response);
	}

	@RequestMapping("/full-length/watch-{id}")
	public ModelAndView moviesPlay(@PathVariable("id") String movieId, 
			HttpServletResponse response) throws IOException {
		return getFullMovieView("/i18n/movie/play/youtube_player", movieId, response);
	}
	
	private ModelAndView getFullMovieView(String viewName, String movieId, 
			HttpServletResponse response) throws IOException{		
		IMovieWebHandler handler = getMovieWebHandler();
		Movie movie = handler.getMovie(movieId);
		if(movie == null){
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		
		ModelAndView mv = new ModelAndView(viewName);
		mv.addObject("movie", movie);
		mv.addObject("movieGenres", getI18nMovieGenres());
		mv.addObject("category", "full-length");
		return mv;
	}
	
	@RequestMapping("/full-length/relate/{id}")
	public ModelAndView moviesRelate(@PathVariable("id") String movieId) throws IOException {
		ModelAndView mv = new ModelAndView("/i18n/movie/play/youtube_relate_video");
		IMovieWebHandler movieWebHandler = getMovieWebHandler();
		QueryItemParams  params = new QueryItemParams(false);
		params.setSort(SORT_TIME);
		MovieItems movieItems = movieWebHandler.listI18nMovies(params, params.getPageInfo(RELATE_PAGE_SIZE));
		List<Movie> list = new ArrayList<Movie>();
		for(Movie movie : movieItems.getItems()){
			if(!movie.getId().equals(movieId)&&list.size() < 5){
				list.add(movie);
			}
		}
		mv.addObject("movieItems", list);
		return mv;
	}
	
	//=========trailer=======
	@RequestMapping("/trailer")
	public void f3(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/world/movie/trailer/");		
	}
	
	@RequestMapping("/trailer/")
	public ModelAndView trailersAll() {
		ModelAndView mv = getListTrailersView(new QueryItemParams(true), "/i18n/movie/trailers/trailers_index");
		mv.addObject("category", "trailer");
		return mv;
	}
	
	@RequestMapping("/trailers")
	public ModelAndView trailersAlls() {
		ModelAndView mv = new ModelAndView("/i18n/movie/trailers/trailers_index");
		mv.addObject("category", "trailer");
		mv.addObject("movieGenres", getI18nTrailerGenres());
		return mv;
	}
	
	@RequestMapping("/trailer/genre/{genre}")
	public ModelAndView queryTrailersGenre(@PathVariable("genre") String genre) {
		QueryItemParams  params = new QueryItemParams(true);
		params.setGenre(genre);
		ModelAndView mv = getListTrailersView(params, "/i18n/movie/trailers/trailers_index");
		mv.addObject("category", "trailer");
		return mv;
	}
	
	@RequestMapping("/trailer/alpha/{alpha}")
	public ModelAndView alpha2(@PathVariable("alpha") String initial) {
		QueryItemParams  params = new QueryItemParams(true);
		params.setInitial(initial);
		ModelAndView mv = getListTrailersView(params, "/i18n/movie/trailers/trailers_index");
		mv.addObject("category", "trailer");
		return mv;
	}

	@RequestMapping("/trailer/{id}")
	public ModelAndView trailersDetail(@PathVariable("id") String movieId,
			HttpServletResponse response) throws IOException {
		return trailerViews("i18n/movie/detail/detail_index", movieId, response);
	}

	@RequestMapping("/trailer/watch-{id}")
	public ModelAndView trailersPlay(@PathVariable("id") String movieId,
			HttpServletResponse response) throws IOException {
		return trailerViews("/i18n/movie/play/imdb_player", movieId, response);
	}
	
	private ModelAndView trailerViews(String viewName, String movieId, 
			HttpServletResponse response) throws IOException {
		IMovieWebHandler handler = getMovieWebHandler();
		Movie movie = handler.getMovie(movieId);
		if(movie == null){
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		
		ModelAndView mv = new ModelAndView(viewName);
		mv.addObject("movie", movie);
		mv.addObject("movieGenres", getI18nTrailerGenres());
		mv.addObject("category", "trailer");
		return mv;
	}

	@RequestMapping("/trailer/query")
	public ModelAndView queryTrailers(QueryItemParams params) {
		params.setNeedGenres(false);
		return getListTrailersView(params, "i18n/movie/trailers/trailers_list");
	}

	private ModelAndView getListMovieView(QueryItemParams params, String viewName) {
		ModelAndView mv = new ModelAndView(viewName);
		IMovieWebHandler movieWebHandler = getMovieWebHandler();
		//params.setSort(SORT_TIME);
		MovieItems movieItems = movieWebHandler.listI18nMovies(params, params.getPageInfo(PAGE_SIZE));
		if (movieItems != null) {
			if (movieItems.getItems().size() == PAGE_SIZE) {
				mv.addObject("hasMore", true);
			}
			if (params.isNeedGenres()) {
				mv.addObject("movieGenres", movieItems.getGenres());
			}
		}
		mv.addObject("movieItems", movieItems);
		return mv;
	}

	private ModelAndView getListTrailersView(QueryItemParams params, String viewName) {
		ModelAndView mv = new ModelAndView(viewName);
		IMovieWebHandler movieWebHandler = getMovieWebHandler();
		params.setSort(SORT_TIME);
		MovieItems movieItems = movieWebHandler.listI18nMovieTraillers(params, params.getPageInfo(PAGE_SIZE));
		if (movieItems != null) {
			if (movieItems.getItems().size() == PAGE_SIZE) {
				mv.addObject("hasMore", true);
			}
			if (params.isNeedGenres()) {
				mv.addObject("movieGenres", movieItems.getGenres());
			}
		}
		mv.addObject("movieItems", movieItems);
		return mv;
	}

	private IMovieWebHandler getMovieWebHandler() {
		Remoting<IMovieWebHandler> remoting = new Remoting<IMovieWebHandler>();
		return remoting.createService(IMovieWebHandler.class);
	}

	private List<Genre> getI18nMovieGenres() {
		Remoting<IMovieWebHandler> remoting = new Remoting<IMovieWebHandler>();
		IMovieWebHandler handler = remoting.createService(IMovieWebHandler.class);
		return handler.listI18nMovieGenres();
	}
	
	private List<Genre> getI18nTrailerGenres() {
		Remoting<IMovieWebHandler> remoting = new Remoting<IMovieWebHandler>();
		IMovieWebHandler handler = remoting.createService(IMovieWebHandler.class);
		return handler.listI18nTrailerGenres();
	}
}
